﻿$().ready(function () {
    InitInput();
    CheckRegister($('#form1'));
});

function InitInput() {
    $('input:visible').each(function () {
        if (this.type == 'text' || this.type == 'password') {
            $(this).blur(function () {
                $(this).removeClass('focus');
                switch (this.name) {
                    case 'Name': CheckUserName(this); break;
                    case 'pwd': CheckPassword(this); break;
                    case 'Email': CheckEmail(this); break;
                    case 'Phone': CheckMobile(this); break;
                }
            }).focus(function () {
                $(this).addClass('focus');
            });
        }
    });
};
function CheckRequire(obj) {
    if (typeof obj == 'undefined') return true;
    if (obj.value == '') {
        $(obj).addClass('err').next().addClass('tip_red');
        return false;
    }
    else {
        $(obj).removeClass('err').next().removeClass('tip_red');
        return true;
    }
};

var tempI = 0;
function CheckRegister(form) {
    $(form).submit(function () {
        if (!CheckUserName(this.Name)) { this.Name.focus(); return false; }
        else if (!CheckPassword(this.pwd)) { this.pwd.focus(); return false; }
        else if (!confirmPassword(this.confirmPassword, this.pwd)) { this.confirmPassword.focus(); return false; }
        else if (!CheckEmail(this.Email)) { this.Email.focus(); return false; }
        else if (!CheckRequire(this.CompanyName)) { this.CompanyName.focus(); return false; }
        else if (!CheckRequire(this.nick_name)) { this.nick_name.focus(); return false; }
        else if (!CheckRequire(this.WebShortName)) { this.WebShortName.focus(); return false; }
        else if (!CheckRequire(this.SiteUrl)) { this.SiteUrl.focus(); return false; }
        else if (!CheckRequire(this.Linkman)) { this.Linkman.focus(); return false; }
        //else if ((!CheckRequire(this.Phone) || !CheckPhone(this.Phone))) { this.Phone.focus(); return false; }
        else if (this.VerifyCode.value == "") {
            this.VerifyCode.focus();
            return false;
        }


        if (!CheckMobile(this.Phone)) {
            $("#msg1").html("手机号填写有误").show().addClass("msg").removeClass("msg2");
            return false;
        }
        form = this;
        //注册一次就隐藏了
        $("#msg1").html("正在注册中，请稍候。").show().addClass("msg").removeClass("msg2");

        var g = $("#g").val();
        var a = $("#a").val();
        $(this).ajaxSubmit({
            url: '/client/ajax/user.ashx?regist&g=' + g + '&a=' + a,
            cache: false,
            dataType: 'json',
            error: function (data) {
                alert('连接服务器失败：' + data.status);
            },
            success: function (data) {
                if (jQuery.isArray(data)) {
                    var s = "";
                    $(data).each(function () {
                        s += this.msg + "\n";
                    });
                    $("#msg1").html(s).show().addClass("msg").removeClass("msg2");
                }
                else {
                    if (data.code == 0) {
                        $("#msg1").html("注册成功，正在登陆后台。").show().addClass("msg").removeClass("msg2");
                        document.location.href = "index.aspx";
                    }
                    else {
                        $("#msg1").html(data.msg).show().addClass("msg2").removeClass("msg");
                        ShowVerifyimg('reg', 'VerifyImg', 'VerifyCode');
                    }
                }
                form.VerifyCode.value = "";
            }
        });

        return false;
    });

};
function CheckEmail(obj) {
    if (!/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/gi.test(obj.value)) {
        $(obj).addClass('err').next().addClass('tip_red');
        return false;
    }
    else {
        $(obj).removeClass('err').next().removeClass('tip_red');
        return true;
    }
}
function CheckPassword(obj) {
    if (obj.value.length < 6) {
        $(obj).addClass('err').next().addClass('tip_red');
        return false;
    }
    else {
        $(obj).removeClass('err').next().removeClass('tip_red');
        return true;
    }
};
function confirmPassword(obj, obj2) {
    if (obj.value != obj2.value) {
        $(obj).addClass('err').next().addClass('tip_red');
        return false;
    }
    else {
        $(obj).removeClass('err').next().removeClass('tip_red');
        return true;
    }
};
function CheckMobile(obj) {
    if (!/^(1[3578]\d{9})$/.test(obj.value)) {       
        $(obj).addClass('err').next().addClass('tip_red');
        return false;
    } else {
        $(obj).removeClass('err').next().removeClass('tip_red');
        return true;
    }
};
function CheckPhone(obj) {
    if (!/^(\d{3,4}-?)?(\d{7,8})(-\d{3})?$/.test(obj.value)) {
        $(obj).addClass('err').next().addClass('tip_red');
        return false;
    } else {
        $(obj).removeClass('err').next().removeClass('tip_red');
        return true;
    }
};
function CheckUserName(obj) {
    if (!/^[a-zA-Z][a-zA-Z0-9_]{3,19}$/gi.test(obj.value)) {
        $(obj).addClass('err').next().addClass('tip_red');
        return false;
    }
    else {
        $(obj).removeClass('err').next().removeClass('tip_red');
        return true;
    }
};

function ShowVerifyimg(sn, img, input, form) {
    if (typeof (form) == "string") form = document.getElementById(form);
    if (typeof (img) == "string") {
        if (form) {
            var tmp = form.getElementsByTagName("SPAN");
            for (var i = 0; i < tmp.length; i++) {
                if (tmp[i].id == img) img = tmp[i];
            }
        }
        else {
            img = document.getElementById(img);
        }
    }
    img.innerHTML = "<img src=\"verifyimage.aspx?sb=1&st=1&sl=1&w=70&h=26&fs=14&key=" + sn + "&" + Math.random() + "\" alt=\"\" width=\"90\" align=\"absMiddle\" style=\"cursor: pointer;\" /> 看不清？点击换一张";

    if (typeof (input) == "string") {
        if (form) {
            var tmp = form.getElementsByTagName("INPUT");
            for (var i = 0; i < tmp.length; i++) {
                if (tmp[i].name == input) input = tmp[i];
            }
        }
        else {
            input = document.getElementById(input);
        }
    }
};
